import 'package:distribution_mall_flutter/constant/http_constant.dart';
import 'package:distribution_mall_flutter/entity/common/question_entity.dart';
import 'package:distribution_mall_flutter/entity/common/upload_file_entity.dart';
import 'package:distribution_mall_flutter/http/init.dart';
import 'package:distribution_mall_flutter/http/response_data.dart';
import 'package:distribution_mall_flutter/pages/agreement/presentation/domain/privacy_entity.dart';
import 'package:distribution_mall_flutter/pages/home/domain/home_entity.dart';
import 'package:uuid/uuid.dart';
import 'package:path/path.dart';

class CommonApi {
  // 获取问题详情
  static Future commonQuestion(Map<String, dynamic> params) async {
    ResponseJsonBody response = await Request(
            HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/help/question/v2/getQuestionDetailByIndexKey')
        .post(params);
    if (response.status?.success != true) return;
    return QuestionEntity.fromJson(response.body);
  }

  // 获取问题详情
  static Future<Result<PrivacyBody?>> commonQuestionT(String indexKey) {
    return Request(HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/help/question/v2/getQuestionDetailByIndexKey')
        .request<PrivacyBody>(
            httpMethod: HttpMethod.post, params: {'indexKey': indexKey});
  }

  // cms信息
  static Future cmsInfo(Map<String, dynamic> params) async {
    return await Request(HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/cms/getCmsInfo')
        .post(params);
  }

  // appInfo信息
  static Future getAppInfo(Map<String, dynamic> params) async {
    ResponseJsonBody response = await Request(
            HttpConstant.getDomain.mallManagerApiGatewayUrl,
            '/distribution-mall-biz/app-api/getAppInfo')
        .get(params);
    if (response.status?.success != true) return;
    return AppInfoEntity.fromJson(response.body);
  }

  //上传单个图片
  static Future<String?> uploadSingleFile({
    required UploadFileInfo file,
  }) async {
    ///获取文件后缀名
    var result = await Request(HttpConstant.getDomain.userCenterApiGatewayUrl,
            '/common-open-biz/basic-data-center/file/uploadImg?fileName=${const Uuid().v4().toString()}${extension(file.path!)}')
        .request<UploadFileBody>(
            httpMethod: HttpMethod.post,
            multipartFile: [file],
            showLoadingIndicator: true);
    return result.data?.link;
  }
}
